<?php
include('includes/connexion.php');
$adminmanager = new AdminManager($connexion);
$elevemanager = new EleveManager($connexion);
session_start();
include('includes/secu.php');
?>
<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
  <title>Tables Advanced - Target Admin</title>

  <meta charset="utf-8">
  <meta name="description" content="">
  <meta name="viewport" content="width=device-width">

  <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Open+Sans:400italic,600italic,700italic,400,600,700">
  <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Oswald:400,300,700">
  <link rel="stylesheet" href="./css/font-awesome.min.css">
  <link rel="stylesheet" href="./js/libs/css/ui-lightness/jquery-ui-1.9.2.custom.min.css">
  <link rel="stylesheet" href="./css/bootstrap.min.css">

  <!-- Plugin CSS -->
  <link rel="stylesheet" href="./js/plugins/icheck/skins/minimal/blue.css">
  <link rel="stylesheet" href="js/plugins/select2/select2.css">
  <link rel="stylesheet" href="js/plugins/datepicker/datepicker.css">
  <link rel="stylesheet" href="js/plugins/simplecolorpicker/jquery.simplecolorpicker.css">
  <link rel="stylesheet" href="js/plugins/timepicker/bootstrap-timepicker.css">
  <link rel="stylesheet" href="js/plugins/fileupload/bootstrap-fileupload.css">
  
  <!-- App CSS -->
  <link rel="stylesheet" href="./css/target-admin.css">
  <link rel="stylesheet" href="./css/custom.css">


  <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
  <!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
  <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
  <![endif]-->
</head>

<?php 
	$tabTest = array(); // tableau des tests
	$testmanager = new TestManager($connexion);// on instancie un test avec la connexion en parametre
	$tabTest = $testmanager->getList(); // on récupere la liste des tests dans le tableau
	
	// si les champs del et id existent on supprimer le test.
	if(!empty($_GET['del']) && !empty($_GET['id'])){
		$stest = $testmanager->get($_GET["id"]);
		$testmanager->delete($stest); // on supprime le test
		header('Location:gestion.php'); // on redirige		
	}  	
	
	// on update un champ
	if(!empty($_POST['IntituleUpdate']) && !empty($_POST['DureeUpdate'])){
		$mtest = new Test();
		$mtest->setId($_POST["idTestUpdate"]);	
		$mtest->setIntitule($_POST['IntituleUpdate']);
		$mtest->setDuree($_POST['DureeUpdate']);
		$testmanager->update($mtest);	
		header("Location:gestion.php"); // on redirige		
	}	
	
	
    // si les champs sont remplis on ajoute à la bdd         	
	if(!empty($_POST['nom']) && !empty($_POST['duree'])){
		$monTest = new Test(); // on créé un nouveau test
		$monTest->setIntitule($_POST['nom']); // on ajoute l'intitulé au test
		$monTest->setDuree($_POST['duree']); // on ajoute la duree au test
		$testmanager->add($monTest); // on ajoute le test à la bdd
		header('Location:gestion.php'); // on redirige			
	}else{
?>

<body>

<?php 
	include('includes/navbar.php');
	include('includes/mainbar.php'); 
?>

<div class="container">

  <div class="content">

    <div class="content-container">

      

      <div class="content-header">
        <h2 class="content-header-title">Tests</h2>
      </div> <!-- /.content-header --> 

      <div class="row">

        <div class="col-md-12">

          <div class="portlet">

            <div class="portlet-header">

              <h3>
                <i class="fa fa-table"></i>
                Liste des tests
              </h3>

            </div> <!-- /.portlet-header -->

            <div class="portlet-content">           

              <div class="table-responsive">


	  <table    class="table table-striped table-bordered table-hover table-highlight table-checkable" 
                data-provide="datatable" 
                data-display-rows="10"
                data-info="true"
                data-search="true"
                data-length-change="true"
                data-paginate="false">
        <thead>        
	    <tr>
        
         
            <th data-filterable="false" data-sortable="true" data-direction="desc">OPTION</th>
            <th data-filterable="false" data-sortable="true" data-direction="desc">NOM</th>
            <th data-filterable="false" data-sortable="true" data-direction="desc">DUREE (heures)</th>
        
        </tr>
        </thead>
        <tbody>
        <?php
       	foreach($tabTest as $test){
			echo "<tr id='test".$test->id()."'>";
			echo "<td align='center'>";
					?><a data-toggle="modal" href="#ModifTest" class="fa fa-gear" id="test<?php echo $test->id(); ?>"></a> | <a href ='?del=1&id=<?php echo $test->id(); ?>' style='color:red;' onclick="return(confirm('Etes-vous sûr de vouloir supprimer ce Test?'));"><span class="fa fa-trash-o"></span></a><?php
			echo " | <a href ='exercice.php?idTest=".$test->id()."'>Gestion des exercices</a>
					
				  </td>";
			echo "<td>".$test->intitule()."</td>";
			echo "<td>".$test->duree()."</td>";
			echo "</tr>";
		}
		?>        
        </tbody>
	    <tr>
         
          	<form action="gestion.php" method="post">
            	
            	  <td align='center'><input type="submit" class="btn btn-success" value="Ajouter un Test"/></td>               
                  <td><input type="text" name="nom" /></td>
                  <td><input type="text" name="duree"/></td>
            </form>
         
        </tr>     
      </table>
              </div> <!-- /.table-responsive -->

            </div> <!-- /.portlet-content -->

          </div> <!-- /.portlet -->

        </div> <!-- /.col -->

      </div> <!-- /.row -->

    </div> <!-- /.content-container -->
      
  </div> <!-- /.content -->

</div> <!-- /.container -->


<?php include('includes/footer.php'); ?>

  <script src="./js/libs/jquery-1.10.1.min.js"></script>
  <script src="./js/libs/jquery-ui-1.9.2.custom.min.js"></script>
  <script src="./js/libs/bootstrap.min.js"></script>

  <!--[if lt IE 9]>
  <script src="./js/libs/excanvas.compiled.js"></script>
  <![endif]-->
  
  <!-- Plugin JS -->
  <script src="./js/plugins/datatables/jquery.dataTables.min.js"></script>
  <script src="./js/plugins/datatables/DT_bootstrap.js"></script>
  <script src="./js/plugins/tableCheckable/jquery.tableCheckable.js"></script>
  <script src="./js/plugins/icheck/jquery.icheck.min.js"></script>

  <!-- App JS -->
  <script src="./js/target-admin.js"></script>
  
<div id="ModifTest" class="modal modal-styled fade">
  <div class="modal-dialog">
    <div class="modal-content">
    <form action="gestion.php" id="mformtest" method="post" class="form parsley-form">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h3 class="modal-title">Modification du test</h3>
      </div>
      <div class="modal-body">
      	<div class="row">
              <div class="col-md-6">
              <label for="IntituleUpdate">Intitulé</label><input type="text" name="IntituleUpdate" />
              </div>
              <div class="col-md-6">
              <label for="DureeUpdate">Durée</label><input type="text" name="DureeUpdate" />
              </div>
          </div>
      </div>
      <div class="modal-footer">
      	<input type="text" name="idTestUpdate" hidden/>
        <button type="button" class="btn btn-tertiary" data-dismiss="modal">Fermer</button>
        <input type="submit" class="btn btn-primary" name="mformtest" value="Sauvegarder"/>
      </div>
      </form>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<script type="text/javascript">
$(document).ready(function() {
	$("a[id^='test']").click(function() {
		
		var id = $(this).attr("id").split("test");
		
		var test3 = '#test'+id[1]+' td+td';
		var test4 = '#test'+id[1]+' td+td+td';
		
		$("#mformtest input[name=idTestUpdate]").val(id[1]);		
				
		$("#mformtest input[name=IntituleUpdate]").val($(test3).html());
		
		$("#mformtest input[name=DureeUpdate]").val($(test4).html());
	});
});
</script>
  
</body>
<?php } ?>
</html>